var t = require("../../../../@babel/runtime/helpers/defineProperty"), a = getApp();

Page({
    data: {
        store_id: "",
        sectionOptions: [],
        uploadUrl: "",
        objectMultiArray: [],
        multiIndex: [ 0, 0, 0 ],
        regional: "请选择商品分类",
        cargon_data: {
            title: "",
            sku: "",
            thumb: "",
            price: "",
            market_price: "",
            cost_price: "",
            stock: ""
        },
        goods_id: "",
        imageUrl: "",
        fileList1: [],
        fileList2: [],
        upNumMax: 0,
        selectedOptions3: [],
        jumpState: !0
    },
    onLoad: function(t) {
        t.store && this.setData({
            store: t.store
        }), t.store_id && this.setData({
            store_id: t.store_id
        }), t.goods_id && this.setData({
            goods_id: t.goods_id
        }), this._urlUp(), this._getClassifyJson();
    },
    onReady: function() {},
    onShow: function() {},
    onHide: function() {},
    onUnload: function() {},
    onPullDownRefresh: function() {},
    onReachBottom: function() {},
    onShareAppMessage: function() {},
    _getClassifyJson: function() {
        var t = this;
        if ("store" == t.data.store) {
            var e = a.getNetAddresss("plugin.store-cashier.frontend.store.goods-category.get-category-by-store-id");
            e += "&store_id=" + t.data.store_id, a._getNetWork({
                url: e,
                success: function(a) {
                    var e = a.data;
                    if (1 == e.result) {
                        for (var i = [], s = 0; s < e.data.list.length; s++) {
                            i.push({
                                id: e.data.list[s].id,
                                name: e.data.list[s].name,
                                children: []
                            });
                            for (var d = 0; d < e.data.list[s].childrens.length; d++) i[s].children.push({
                                id: e.data.list[s].childrens[d].id,
                                name: e.data.list[s].childrens[d].name
                            });
                        }
                        t.setData({
                            sectionOptions: i
                        }), t.goodsHandlefunction();
                    } else wx.showToast({
                        icon: "none",
                        title: e.msg,
                        duration: 1500
                    });
                    t.getGoodsJson();
                },
                fail: function(t) {
                    console.log(t.msg);
                }
            });
        } else if ("supplier" == t.data.store) {
            var i = a.getNetAddresss("plugin.supplier.frontend.goods.get-category");
            a._postNetWork({
                url: i,
                showToastIn: !1,
                data: {},
                success: function(a) {
                    var e = a.data;
                    if (1 == e.result) {
                        for (var i = [], s = 0; s < e.data.length; s++) if (i.push({
                            id: e.data[s].id,
                            name: e.data[s].name,
                            children: []
                        }), !e.data[s].childrens || e.data[s].childrens.length < 1) ; else for (var d = 0; d < e.data[s].childrens.length; d++) if (i[s].children.push({
                            id: e.data[s].childrens[d].id,
                            name: e.data[s].childrens[d].name,
                            children: []
                        }), !e.data[s].childrens[d].childrens || e.data[s].childrens[d].childrens.length < 1) ; else for (var n = 0; n < e.data[s].childrens[d].childrens.length; n++) i[s].children[d].children.push({
                            id: e.data[s].childrens[d].childrens[n].id,
                            name: e.data[s].childrens[d].childrens[n].name
                        });
                        t.setData({
                            sectionOptions: i
                        }), t.goodsHandlefunction();
                    } else wx.showToast({
                        icon: "none",
                        title: e.msg,
                        duration: 1500
                    });
                    t.getGoodsJson();
                },
                fail: function(t) {}
            });
        }
    },
    _urlUp: function() {
        "store" == this.data.store ? this.setData({
            uploadUrl: "plugin.store-cashier.frontend.store.goods.upload"
        }) : "supplier" == this.data.store && this.setData({
            uploadUrl: "plugin.supplier.frontend.goods.upload"
        });
    },
    goodsHandlefunction: function() {
        for (var t = this.data.sectionOptions, a = [], e = [], i = [], s = 0; s < t.length; s++) a.push({
            id: t[s].id,
            name: t[s].name
        });
        if (t[0].children.length > 0) for (var d = 0; d < t[0].children.length; d++) e.push({
            id: t[0].children[d].id,
            name: t[0].children[d].name
        });
        if (t[0].children && t[0].children.length > 0 && t[0].children[0].children && t[0].children[0].children.length > 0) for (var n = 0; n < t[0].children[0].children.length; n++) i.push({
            id: t[0].children[0].children[n].id,
            name: t[0].children[0].children[n].name
        });
        this.setData({
            "objectMultiArray[0]": a,
            "objectMultiArray[1]": e,
            "objectMultiArray[2]": i
        });
    },
    getGoodsJson: function() {
        var t = this, e = "", i = {};
        "store" == t.data.store ? (e = a.getNetAddresss("plugin.store-cashier.frontend.store.goods.get-goods-detail"), 
        i = {
            goods_id: t.data.goods_id
        }) : "supplier" == t.data.store && (e = a.getNetAddresss("plugin.supplier.frontend.goods.goods-detail"), 
        i = {
            goods_id: t.data.goods_id
        }), a._postNetWork({
            url: e,
            showToastIn: !1,
            data: i,
            success: function(a) {
                var e = a.data;
                if (1 == e.result) {
                    var i = t.data.selectedOptions3;
                    if (e.data.category_ids.parentid && i.push(Number(e.data.category_ids.parentid)), 
                    e.data.category_ids.childid && i.push(Number(e.data.category_ids.childid)), e.data.category_ids.thirdid && i.push(Number(e.data.category_ids.thirdid)), 
                    t.setData({
                        selectedOptions3: i
                    }), null != e.data.thumb_url && "" != e.data.thumb_url) {
                        for (var s = t.data.fileList1, d = t.data.fileList2, n = 0; n < e.data.thumb_url.length; n++) s.push({
                            url: e.data.thumb_url[n]
                        }), d.push({
                            url: e.data.thumb_url[n]
                        });
                        t.setData({
                            fileList1: s,
                            fileList2: d,
                            upNumMax: e.data.thumb_url.length
                        });
                    }
                    t.setData({
                        "cargon_data.title": e.data.title,
                        "cargon_data.sku": e.data.sku,
                        imageUrl: e.data.thumb,
                        "cargon_data.thumb": e.data.thumb,
                        "cargon_data.price": e.data.price,
                        "cargon_data.market_price": e.data.market_price,
                        "cargon_data.cost_price": e.data.cost_price,
                        "cargon_data.stock": e.data.stock
                    }), t.commodityInit();
                } else wx.showToast({
                    icon: "none",
                    title: e.msg,
                    duration: 1500
                });
            },
            fail: function(t) {}
        });
    },
    titleinp: function(t) {
        var a = t.detail.value;
        this.setData({
            "cargon_data.title": a
        });
    },
    skuinp: function(t) {
        var a = t.detail.value;
        this.setData({
            "cargon_data.sku": a
        });
    },
    priceinp: function(t) {
        var a = t.detail.value;
        this.setData({
            "cargon_data.price": a
        });
    },
    marketpriceinp: function(t) {
        var a = t.detail.value;
        this.setData({
            "cargon_data.market_price": a
        });
    },
    costPriceinp: function(t) {
        var a = t.detail.value;
        this.setData({
            "cargon_data.cost_price": a
        });
    },
    stockinp: function(t) {
        var a = t.detail.value;
        this.setData({
            "cargon_data.stock": a
        });
    },
    onRead_1: function() {
        if (4 == this.data.upNumMax || this.data.upNumMax >= 4) return wx.showToast({
            icon: "none",
            title: "图片数量已达到上限",
            duration: 1500
        }), !1;
        var t = this, e = wx.getStorageSync("wx_token"), i = wx.getStorageSync("session"), s = a.getNetAddresss(this.data.uploadUrl);
        s += "&3rd_session=" + i, wx.chooseImage({
            count: 1,
            sizeType: [ "original", "compressed" ],
            sourceType: [ "album", "camera" ],
            success: function(a) {
                var i = a.tempFilePaths;
                console.log(i[0]), wx.compressImage({
                    src: i[0],
                    quality: 60,
                    success: function(a) {
                        var i = a.tempFilePath;
                        wx.uploadFile({
                            url: s,
                            filePath: i,
                            name: "file",
                            formData: {
                                attach: "upload"
                            },
                            header: {
                                "Content-Type": "multipart/form-data",
                                Cookie: "PHPSESSID=" + e
                            },
                            success: function(a) {
                                var e = JSON.parse(a.data);
                                if (1 == e.result) {
                                    var s = t.data.fileList1, d = t.data.fileList2;
                                    s.push({
                                        url: e.data.img
                                    }), d.push({
                                        url: i
                                    }), t.setData({
                                        upNumMax: t.data.upNumMax + 1,
                                        fileList1: s,
                                        fileList2: d
                                    });
                                } else wx.showToast({
                                    icon: "none",
                                    title: e.msg,
                                    duration: 1500
                                });
                            }
                        });
                    },
                    fail: function(t) {},
                    complete: function(t) {}
                });
            }
        });
    },
    removeImg: function(t) {
        var a = t.currentTarget.dataset.index, e = this.data.fileList1, i = this.data.fileList2;
        e.splice(a, 1), i.splice(a, 1), this.setData({
            fileList1: e,
            fileList2: i
        }), this.setData({
            upNumMax: this.data.fileList1.length
        });
    },
    commodityInit: function() {
        for (var t = this.data.selectedOptions3, a = this.data.sectionOptions, e = [], i = [], s = "", d = 0; d < a.length; d++) if (a[d].id == t[0]) {
            this.setData({
                "multiIndex[0]": d
            }), s += a[d].name;
            break;
        }
        if (a[this.data.multiIndex[0]].children.length > 0) for (var n = 0; n < a[this.data.multiIndex[0]].children.length; n++) e.push({
            id: a[this.data.multiIndex[0]].children[n].id,
            name: a[this.data.multiIndex[0]].children[n].name
        });
        if (this.setData({
            "objectMultiArray[1]": e
        }), a[this.data.multiIndex[0]].children.length > 0) for (var r = 0; r < a[this.data.multiIndex[0]].children.length; r++) if (a[this.data.multiIndex[0]].children[r].id == t[1]) {
            this.setData({
                "multiIndex[1]": r
            }), s += "/" + a[this.data.multiIndex[0]].children[r].name;
            break;
        }
        if (a[this.data.multiIndex[0]].children && a[this.data.multiIndex[0]].children.length > 0 && a[this.data.multiIndex[0]].children[this.data.multiIndex[1]].children && a[this.data.multiIndex[0]].children[this.data.multiIndex[1]].children.length > 0) for (var o = 0; o < a[this.data.multiIndex[0]].children[this.data.multiIndex[1]].children.length; o++) i.push({
            id: a[this.data.multiIndex[0]].children[this.data.multiIndex[1]].children[o].id,
            name: a[this.data.multiIndex[0]].children[this.data.multiIndex[1]].children[o].name
        });
        if (this.setData({
            "objectMultiArray[2]": i
        }), a[this.data.multiIndex[0]].children && a[this.data.multiIndex[0]].children.length > 0 && a[this.data.multiIndex[0]].children[this.data.multiIndex[1]].children && a[this.data.multiIndex[0]].children[this.data.multiIndex[1]].children.length > 0) for (var l = 0; l < a[this.data.multiIndex[0]].children[this.data.multiIndex[1]].children.length; l++) if (a[this.data.multiIndex[0]].children[this.data.multiIndex[1]].children[l].id == t[2]) {
            this.setData({
                "multiIndex[1]": l
            }), s += "/" + a[this.data.multiIndex[0]].children[this.data.multiIndex[1]].children[l].name;
            break;
        }
        this.setData({
            regional: s
        });
    },
    commodityBtn: function(a) {
        var e = a.detail.column, i = a.detail.value, s = {
            multiArray: this.data.objectMultiArray
        };
        switch (this.setData(t({}, "multiIndex[" + e + "]", i)), e) {
          case 0:
            this.selecommodity_0(i);
            break;

          case 1:
            this.selecommodity_1(this.data.multiIndex[0], i);
        }
        this.setData(s);
    },
    commodityconfirm: function(t) {
        var a = this.data.sectionOptions, e = t.detail.value, i = e[0], s = e[1], d = e[2], n = [], r = [];
        a.length > 0 && (n.push(a[i].id), r.push(a[i].name), a[i].children.length > 0 && (n.push(a[i].children[s].id), 
        r.push(a[i].children[s].name), a[i].children[s].children && a[i].children[s].children.length > 0 && (n.push(a[i].children[s].children[d].id), 
        r.push(a[i].children[s].children[d].name)))), this.setData({
            regional: r.join("/"),
            selectedOptions3: n
        });
    },
    selecommodity_0: function(t) {
        var a = this.data.sectionOptions, e = [], i = [];
        if (a[t].children.length > 0) for (var s = 0; s < a[t].children.length; s++) e.push({
            id: a[t].children[s].id,
            name: a[t].children[s].name
        });
        if (a[t].children && a[t].children.length > 0 && a[t].children[0].children && a[t].children[0].children.length > 0) for (var d = 0; d < a[t].children[0].children.length; d++) i.push({
            id: a[t].children[0].children[d].id,
            name: a[t].children[0].children[d].name
        });
        this.setData({
            "objectMultiArray[1]": e,
            "objectMultiArray[2]": i,
            "multiIndex[1]": 0,
            "multiIndex[2]": 0
        });
    },
    selecommodity_1: function(t, a) {
        var e = this.data.sectionOptions, i = [];
        if (e[t].children && e[t].children.length > 0 && e[t].children[a].children && e[t].children[a].children.length > 0) for (var s = 0; s < e[t].children[a].children.length; s++) i.push({
            id: e[t].children[a].children[s].id,
            name: e[t].children[a].children[s].name
        });
        this.setData({
            "objectMultiArray[2]": i,
            "multiIndex[2]": 0
        });
    },
    confirmationEditor: function() {
        if (this.jumpEvent(), 1 == this.data.jumpState) {
            var t = [];
            if (0 != this.data.fileList1.length) for (var e = 0; e < this.data.fileList1.length; e++) t.push(this.data.fileList1[e].url); else t = [];
            var i = "", s = {};
            if ("store" == this.data.store) i = a.getNetAddresss("plugin.store-cashier.frontend.store.goods.edit-goods"), 
            s = {
                store_id: this.data.store_id,
                goods_id: this.data.goods_id,
                category_pid: this.data.selectedOptions3[0],
                category_cid: this.data.selectedOptions3[1],
                title: this.data.cargon_data.title,
                thumb: this.data.cargon_data.thumb,
                thumb_url: t,
                cost_price: this.data.cargon_data.cost_price,
                market_price: this.data.cargon_data.market_price,
                price: this.data.cargon_data.price,
                sku: this.data.cargon_data.sku,
                stock: this.data.cargon_data.stock
            }; else if ("supplier" == this.data.store) {
                if (i = a.getNetAddresss("plugin.supplier.frontend.goods.edit-goods"), s = {
                    title: this.data.cargon_data.title,
                    goods_id: this.data.goods_id,
                    thumb: this.data.cargon_data.thumb,
                    thumb_url: t,
                    cost_price: this.data.cargon_data.cost_price,
                    market_price: this.data.cargon_data.market_price,
                    price: this.data.cargon_data.price,
                    sku: this.data.cargon_data.sku,
                    stock: this.data.cargon_data.stock
                }, this.data.selectedOptions3.length < 2) return void wx.showToast({
                    icon: "none",
                    title: "必须选择二级分类",
                    duration: 1500
                });
                this.data.selectedOptions3.length < 3 ? (s.category_pid = this.data.selectedOptions3[0], 
                s.category_cid = this.data.selectedOptions3[1]) : (s.category_pid = this.data.selectedOptions3[0], 
                s.category_cid = this.data.selectedOptions3[1], s.category_tid = this.data.selectedOptions3[2]);
            }
            a._postNetWork({
                url: i,
                showToastIn: !1,
                data: s,
                success: function(t) {
                    var a = t.data;
                    1 == a.result ? wx.showToast({
                        title: "上传商品成功",
                        icon: "none",
                        duration: 1500,
                        success: function() {
                            setTimeout(function() {
                                wx.navigateBack({
                                    delta: 1
                                });
                            }, 1500);
                        }
                    }) : wx.showToast({
                        icon: "none",
                        title: a.msg,
                        duration: 1500
                    });
                },
                fail: function(t) {}
            });
        } else wx.showToast({
            icon: "none",
            title: "商品信息不完整",
            duration: 1500
        });
    },
    jumpEvent: function() {
        for (var t in this.setData({
            jumpState: !0
        }), this.data.cargon_data) "" == this.data.cargon_data[t] && this.setData({
            jumpState: !1
        });
        0 == this.data.selectedOptions3.length && this.setData({
            jumpState: !1
        }), "" == this.data.imageUrl && this.setData({
            jumpState: !1
        });
    },
    onRead: function() {
        var t = this, e = a.getNetAddresss(this.data.uploadUrl);
        wx.chooseImage({
            count: 1,
            sizeType: [ "original", "compressed" ],
            sourceType: [ "album", "camera" ],
            success: function(a) {
                var i = a.tempFilePaths;
                wx.compressImage({
                    src: i[0],
                    quality: 60,
                    success: function(a) {
                        var i = a.tempFilePath;
                        wx.uploadFile({
                            url: e,
                            filePath: i,
                            name: "file",
                            success: function(a) {
                                var e = JSON.parse(a.data);
                                1 == e.result ? t.setData({
                                    imageUrl: i,
                                    "cargon_data.thumb": e.data.img
                                }) : wx.showToast({
                                    icon: "none",
                                    title: e.msg,
                                    duration: 1500
                                });
                            }
                        });
                    },
                    fail: function(t) {},
                    complete: function(t) {}
                });
            }
        });
    }
});